﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using KL.UFrame.Api.Extensions;
using KL.UFrame.Core.Unity;
using KL.UFrame.Data.Business;
using KL.UFrame.Data.Models;

namespace KL.UFrame.Api.Controllers
{
   
    public class VenueController : K9ControllerBase
    {
        private IVenueBusiness _business = UnityBootstrapper.Current.GetService<IVenueBusiness>();
        // GET: Venue
        public async Task<ViewResult> Index()
        {
            var model =await _business.GetAllVenuesAsync();
            return View(model);
        }

        public async Task<ViewResult> Book(int id)
        {
            var venue = await _business.GetVenueAsync(id);
            if (venue == null) return NotFound();
            return View(venue);
        }

        public ViewResult Record()
        {
            var model = new VenueBookRecordSearchModel
            {
                Start = DateTime.Today.ToMonthFirst(),
                End = DateTime.Today.ToMonthLast()
            };
            return View(model);
        }
        [HttpPost]
        public async Task<ViewResult> Record(VenueBookRecordSearchModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Start.AddMonths(3) < model.End)
                {
                    ModelState.AddModelError("", "查询跨度不能超过3个月");
                }
                else
                {
                    model.Results = await _business.GetVenueBookRecordsAsync(0, model.Phone, model.Start, model.End);
                }
            }
            return View(model);
        }

        public async Task<ViewResult> Result()
        {
            var appBusiness = UnityBootstrapper.Current.GetService<IAppConfigBusiness>();

            return View(new ResultModel
            {
                Title = "预定成功",
                Content = await appBusiness.GetContentByKeyAsync(AppConfigKeys.VenueBookSuccess.ToString()),
                ReturnUrl = Url.Action("Index")
            });
        }
    }
}